package it.szy.com.utl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionutls {
    public static SqlSessionFactory sessionFactory;

    static {
        //获取配置文件输入流
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//创建SqlSessionFactoryBuilder会话工厂构建类（建筑公司）
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new
                SqlSessionFactoryBuilder();
//创建SqlSessionFactory会话工厂
        sessionFactory =
                sqlSessionFactoryBuilder.build(inputStream);
    }


    public static SqlSession getSession() {
        SqlSession sqlSession = sessionFactory.openSession();
        return sqlSession;
    }

    public static SqlSession getSession(boolean auotcomit) {
        SqlSession sqlSession = sessionFactory.openSession(auotcomit);
        return sqlSession;
    }


    public static  void close(SqlSession session) {
        session.close();

    }

}
